<?php namespace Admin\Controller;
//商品控制器
class GoodsController extends CommonController
{
    private $model;private $cate;private $brand;private $typeAttr;private $type;private $lnventory;private $goodsAttr;

    public function __auto()
    {
       $this->model = new \Common\Model\Goods;
       $this->cate = new \Common\Model\Cate;
       $this->brand = new \Common\Model\Brand;
       $this->typeAttr = new \Common\Model\TypeAttr;
       $this->type = new \Common\Model\Type;
       $this->lnventory = new \Common\Model\Lnventory;
       $this->goodsAttr = new \Common\Model\GoodsAttr;
    }

    //显示品牌列表首页
    public function index()
    {
        // 如果列表没有，跳到添加页面
       $data=$this->model->get();
       // P($data);
       if (!$data) {
           View::success('请先去添加类型', U('add'));
       }
       View::with('data', $data);
        View::make();
    }
    public function add(){
        //获取各个选择项所需要的数据
        // $cate=new \Common\Model\Cate;
        $cateData=$this->cate->get();
        $cateData=Data::tree($cateData,'cname');
        // P($cateData);die;
        // $brand=new \Common\Model\Brand;
        $brandData=$this->brand->get();
        if(IS_POST){
            if($this->model->store()){
                View::success('添加成功',U('index'));
            }
            View::error($this->model->getError());
        }
        
        // View::with('typeData',$typeData);
        View::with('brandData',$brandData);
        View::with('cateData',$cateData);
        View::make();
    }
    //异步接受函数
    public function sel(){
        $cid=Q('post.f',0,'intval');
        $zhi=$this->cate->where("cid={$cid}")->pluck('type_typeid');
        //异步接受数据 处理遍历数据
        $taData=$this->typeAttr->where("type_typeid={$zhi}")->get();
        foreach($taData as $k=>$v){
            $taData[$k]['tavalue']=explode('|',$v['tavalue']);
        }
        // P($taData);die;
        echo json_encode($taData);exit;
    }

    //uploadify异步上传
    public function upload()
    {
        $file = Upload::path('Upload/Content/'.date('y/m'))->make();

        if (empty($file)) {
            // 相当于：echo json_encode(Upload::getError());exit;
            $this->ajax(Upload::getError());
        } else {
            /** $file内部就是以下这个数组
                $file = array(
                    0 => array(
                        'path' => 'Upload/Content/15/8/123981239172.jpg'    ,
                        'url' => 'http://localhost/cms_edu/Upload/Content/15/8/123981239172.jpg',
                        'image' => 1
                ),
            );**/
            
            //上传成功，把上传好的信息返给js 也就是uploadify
            $data = $file[0];
            // 相当于：echo json_encode($data);exit;
            $this->ajax($data); 
        }
    }
    //删除添加商品时没有提交的图片
    public function delpic(){ 
//                 P(Q('post.c'));
                unlink(Q('post.c'));      
    }
    //删除商品
    public function del(){
        //获得当前商品id
        $gid=Q('get.gid',0,'intval');
        //删除相关商品详情数据
        $lpath=$this->lnventory->where("goods_gid={$gid}")->pluck('picture');
        if($lpath){
            unlink($lpath);
        }
        $this->lnventory->where("goods_gid={$gid}")->delete();
        //删除列表图
        $gpath=$this->model->where("gid={$gid}")->pluck('listpic');
        if($gpath){
            unlink($gpath);
        }
        // 删除列表数据
        $this->model->where("gid={$gid}")->delete();
        //删除相关商品属性
        $this->goodsAttr->where("goods_gid={$gid}")->delete();
        View::success("删除成功",U('index'));

    }
    //编辑商品
    public function edit(){
        if(IS_POST){
//             P(Q('post.'));die;
            if($this->model->edit()){
                View::success("修改成功",U('index'));
            }
            View::error($this->model->getError());
        }
        //1.获得旧数据
        $gid=Q('get.gid',0,'intval');
        $goodsData=$this->model
                ->leftjoin("lnventory",'goods_gid','=','gid')
                ->where("gid={$gid}")
                ->find();
                // p($goodsData);
        View::with('goodsData',$goodsData);
        // P($goodsData);
        //获取分类数据 getAll直接获取树状结构
        //2.处理分类数据
        $cateData=$this->cate->get();
        $cateData=Data::tree($cateData,'cname');
        View::with('cateData',$cateData);
        //3.处理品牌数据
        $brandData=$this->brand->get();
        View::with('brandData',$brandData);
        //4.处理商品详情数据
        $lnventoryData=$this->lnventory
                    ->where("goods_gid={$gid}")
                    ->find();
        //处理图片详情数据
        $arr=explode('|',$lnventoryData['picture']);
        $lnventoryData['picture']=$arr;
//        P($lnventoryData);
        View::with('lnventoryData',$lnventoryData);
        //5.获取商品属性规格数据
        $data=$this->goodsAttr
            ->rightjoin('typeattr','type_attr_taid','=','taid')
            ->where("goods_gid={$gid}")
            ->get();
        //获得taid就是为了以后排除已经选过的类型属性
        $taid=[];
        // P($data);
        foreach($data as $k=>$v){
            $data[$k]['tavalue']=explode('|',$v['tavalue']);
            $taid[]=$v['taid'];
        }
        // P($goodsData);die;
        //5.获取所有属性中没有选过的属性
        //impload把数组转化为字符串
        $strTaid=implode(',',$taid);
        $taData=$this->typeAttr
                ->where("taid NOT IN ({$strTaid}) AND type_typeid={$goodsData['type_typeid']}")
                ->get();
        // p($taData);
        foreach($taData as $k=>$v){
            $data[]=array(
            'gavalue'=>'',
            'taname'=>$v['taname'],
            'tavalue'=>explode('|',$v['tavalue']),
            'attr_type'=>$v['attr_type'],
            'taid'=>$v['taid']
            );
        }
        // P($data);
        View::with('data',$data);
        View::make();
    }
}